<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
/*群管理*/
class AppGroupController extends AdminbaseController{
	protected $model_model;
	protected $user_model;
	protected $user_group;
	function _initialize() { 
		parent::_initialize();
		$this->model_model = D("Common/AppGroup");
		$this->user_model = D("Common/AppUser");
		$this->user_group=D("Common/UserGroup");
		vendor("UploadHandler");
	}
	
	function index(){
		$this->_list();
		$this->display();
	}
	
	function add(){
		$this->display();
	}
	
	function add_post(){
		if(IS_POST){
			$is_user=$this->user_model->where("user_uniq_id='".I("user_uniq_id",'')."'")->count(1);
			if($is_user!=1){
				$this->error("群主不存在！");
			}
			$data=$this->model_model->create();
			if ($data){
				/*群数据*/
				$option['groupname']=$data['groupname'];
				$option['desc']=$data['descscription'];
				$option['public']=$data['is_public']==1?true:false;
				$option['approval']=true;
				$option['owner']=$data['user_uniq_id'];
				$option['members']=array($data['user_uniq_id']);
				$hx_result=$this->createGroups($option);
				\Think\Log::write("创建群 create_group--- 环信代码--".json_encode($hx_result));
				if(empty($hx_result['error'])){// 环信接口访问正常
					$data['huanxing_groupid']=$hx_result['data']['groupid'];
					$this->model_model->add($data);
					$this->_add_group_user(array($data['user_uniq_id']),$data['group_uniq_id'], $data['user_uniq_id']);
				    $this->success("成功",U("AppGroup/index"));
				}else{// 环信接口访问出错
				   $this->error(L('ADD_FAILED'));
				}
				
			} else {
				$this->error($this->model_model->getError());
			}
		
		}
	}

	function edit_vidio(){
		$this->display();
	}
	function delete_pic(){
		$id=I("id");
		M("Video")->delete($id);
		$this->redirect("AppGroup/upload_pic");
	}
	/*上传图片*/
	function upload_pic(){
		$video=I("video");
		$name=I("name");
		$this->assign("list",M("Video")->select());
		if(empty($video)){
			$this->display();
		}else{
			foreach ($video as $key => $value) {
				$date[]=array("url"=>$value,"name"=>$name[$key]);
			}
			M("Video")->addAll($date);
			$this->redirect("AppGroup/upload_pic");
		}
		
	}

	function upload_post(){
		$upload=new \UploadHandler();
	}
	
	function edit(){
		$id=I("get.id");
		$model=$this->model_model->where("id=$id")->find();
		$this->assign('model',$model);
		$this->assign('group_user_list',$this->model_model->group_user_list($model['group_uniq_id']));
		$this->display();
	}
	
	function edit_post(){
		if (IS_POST) {
			$data=$this->model_model->create();
			$this->model_model->save($data);
				$sql1=M("UserGroup")->where("group_uniq_id='".I('group_uniq_id1')."' and user_uniq_id='".$data['user_uniq_id']."'")->fetchsql(FALSE)->setField(array("level"=>4));
				$sql2=M("UserGroup")->where("group_uniq_id='".I('group_uniq_id1')."' and user_uniq_id='".I('owner_id1')."'")->fetchsql(FALSE)->setField(array("level"=>1));
				$sql3=M("AppGroup")->where("group_uniq_id='".I('group_uniq_id1')."'")->fetchsql(FALSE)->setField(array("user_uniq_id"=>I('owner_id1')));
				$this->success("保存成功！", U("AppGroup/index"));
			} else {
				$this->error($this->model_model->getError());
			}
	}
	
	/**
	 *  删除
	 */
	function delete(){
		$id = I("get.id",0,"intval");
		if ($this->model_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	
	function toggle(){
		if(isset($_POST['ids']) && $_GET["display"]){
			$ids = implode(",", $_POST['ids']);
			$data['status']=1;
			if ($this->model_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("显示成功！");
			} else {
				$this->error("显示失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["hide"]){
			$ids = implode(",", $_POST['ids']);
			$data['status']=0;
			if ($this->model_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("隐藏成功！");
			} else {
				$this->error("隐藏失败！");
			}
		}
	}
	///////////////////////////////////////////////////////////////////////////
		 function _list() {
	    $fields = array('groupname'=>array("field" =>"groupname","operator" =>"like"),
						'user_uniq_id'=>array("field" =>"user_uniq_id","operator" =>"like"),
						'group_uniq_id'=>array("field" =>"group_uniq_id","operator" =>"like"),
						'is_recommend'=>array("field" =>"is_recommend","operator" =>"like")
						);
	    $where_and = $this ->get_where_ands(IS_POST, $fields);
	    $where = join(' and ', $where_and);
	    $count = $this ->model_model -> where($where) ->count(1);
			
	    $page = $this ->page($count, 20);
	    $this ->assign("Page", $page ->show('Admin'));
	    $this ->assign("current_page", $page ->GetCurrentPage());
	    $list = $this ->model_model ->field(true)->where($where) ->order('create_time desc') ->limit($page ->firstRow.','.$page ->listRows) ->select();
		foreach ($list as $key => $value) {
			$group_uniq_ids[]=$value['group_uniq_id'];
		}
		if(!empty($group_uniq_ids)){
			$map['group_uniq_id']=array("in",$group_uniq_ids);
		}
		//$map['group_uniq_id']=array("in",$group_uniq_ids);
		$group_user=$this->user_group->where($map)->group("group_uniq_id")->getField("group_uniq_id,count(1) as count",true);
		
	    $this ->assign('list', $list);
		$this->assign("group_user",$group_user);
	}
	
	///////////////////////////////////////////////////////////////////////
	/**
	 * 添加本地群成员
	 * $user_uniq_ids  用户互动号
	 * $group_uniq_id 	群互动号
	 * $ower_uniq_id 群主互动id
	 */
     function _add_group_user($user_uniq_ids,$group_uniq_id,$ower_uniq_id){
    	foreach ($user_uniq_ids as $key => $value) {
    		if($ower_uniq_id==$value){
    			$datalist[]=array('user_uniq_id'=>$value,'group_uniq_id'=>$group_uniq_id,'level'=>1);	
    		}else{
    			$datalist[]=array('user_uniq_id'=>$value,'group_uniq_id'=>$group_uniq_id,'level'=>3);
    		}
    	}
    	M("UserGroup")->addAll($datalist);
    }
}